package com.gxa.uhome.controller;

import com.gxa.uhome.mapper.SlipMapper;
import com.gxa.uhome.pojo.*;
import com.gxa.uhome.service.UserOrderService;
import com.gxa.uhome.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Laiguowei
 * @date: 10:04 2021/6/9
 **/
@CrossOrigin
@RestController
@RequestMapping("/userOrder")
@Api(tags = "用户订单模块")
public class UserOrderController {


    @Resource
    UserOrderService userOrderService = null;


    @PostMapping("/getDetail")
    @ApiOperation("获得用户订单的详情")
    @ApiImplicitParam(name = "id" ,value = "用户订单id",dataType = "int")
    public JsonResult getDetail( @RequestParam Integer id){
        JsonResult jsonResult = new JsonResult();
        UserOrder detail = userOrderService.getDetail(id);
        if (detail!=null){
            jsonResult.setCode("1");
            jsonResult.setData(detail);
            jsonResult.setMsg("成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }

        return jsonResult;
    }

    @PostMapping("/createUserOrder")
    @ApiOperation("创建用户订单")
    @ApiImplicitParam(name = "userOrder" ,value = "json字符串格式的userOrder对象" ,paramType = "body")
    public JsonResult createUserOrder(@Autowired HttpServletRequest request ,@RequestBody UserOrder userOrder){
        JsonResult jsonResult = new JsonResult();
        Integer integer = userOrderService.createUserOrder(request,userOrder);

        if (integer!=0){
            jsonResult.setCode("1");
            jsonResult.setMsg("成功");

        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");

        }
        return jsonResult;
    }
    @PostMapping("/updateUserOrder")
    @ApiOperation("更新用户订单")
    @ApiImplicitParam(name = "userOrder" ,value = "json字符串格式的userOrder对象",paramType = "body")
    public JsonResult updateUserOrder(@RequestBody UserOrder userOrder){
        JsonResult jsonResult = new JsonResult();
        Integer integer = userOrderService.updateUserOrder(userOrder);
        if (integer!=0){
            jsonResult.setCode("1");
            jsonResult.setMsg("成功");

        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");

        }
        return jsonResult;
    }
    @PostMapping("/selectByPage")
    @ApiOperation("用户订单分页查询（用于站点的分页）")
    @ApiImplicitParam(name = "map" ,value = "json字符串格式的map map里参数名字：必需参数：page size 非必需参数： name status beginTime endTime",paramType = "body")
    public JsonResult selectByPage(@RequestBody Map<String,Object> map){
        JsonResult jsonResult = new JsonResult();
        int size = Integer.parseInt(String.valueOf(map.get("size")));
        int page = Integer.parseInt(String.valueOf(map.get("page")));
        map.put("page",(page-1)*size);
        map.put("size",size);
        List<Map<String,Object>>userOrders = userOrderService.selectByPage(map);
        if (userOrders!=null){
            jsonResult.setCode("1");
            jsonResult.setData(userOrders);
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }

    @PostMapping("/selectByUserId")
    @ApiOperation("用户订单前端订单查询")
    @ApiImplicitParam(name = "id" ,value = "用户的id",dataType = "int",example ="3")
    public JsonResult selectByPage(@RequestParam Integer id){
        JsonResult jsonResult = new JsonResult();
        List<UserOrder> userOrders = userOrderService.selectByUserId(id);
        if (userOrders!=null){
            jsonResult.setCode("1");
            jsonResult.setData(userOrders);
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }


    @PostMapping("/getDetail1")
    @ApiOperation("用户订单前端订单详情")
    @ApiImplicitParam(name = "id" ,value = "订单id",dataType = "int",example ="3")
    public JsonResult getDetail1(@RequestParam Integer id){
        JsonResult jsonResult = new JsonResult();
        UserOrder detail = userOrderService.getDetail1(id);
        List<Order> orders = detail.getOrders();
        List<Slip> slips = detail.getSlips();
        User user = detail.getUser();
        Station station = detail.getStation();
        UserAddress userAddress = detail.getUserAddress();
        Map<String,Object> map = new HashMap<>();
        map.put("user",user);
        map.put("adress",userAddress);
        map.put("station",station);
        map.put("slips",slips);
        map.put("orders",orders);
        if (detail!=null){
            jsonResult.setCode("1");
            jsonResult.setData(detail);
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }

    @PostMapping("/selectOrderCountByStatus")
    @ApiOperation("这个方法将会通过用户的id返回这个用户所有订单状态下的订单数")
    @ApiImplicitParam(name = "id" ,value = "用户id",dataType = "int",example ="1")
    public JsonResult selectOrderCountByStatus(@RequestParam Integer id){
        JsonResult jsonResult = new JsonResult();
        List<Map<Integer, Integer>> maps = userOrderService.selectOrderCountByStatus(id);
        if (maps!=null){
            jsonResult.setCode("1");
            jsonResult.setData(maps);
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }

    @PostMapping("/selectOrderByStatus")
    @ApiOperation("通过用户id和订单状态得到该用户该状态下的所有订单 订单信息包含地址、站点、用户订单信息")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id" ,value = "用户id",dataType = "int",example ="1"), @ApiImplicitParam(name = "status" ,value = "订单状态",dataType = "int",example ="1")}
    )
    public JsonResult selectOrderCountByStatus(@RequestParam Integer id, @RequestParam Integer status){
        JsonResult jsonResult = new JsonResult();
        List<UserOrder> userOrders = userOrderService.selectOrderByStatus(id, status);
        if (userOrders!=null){
            jsonResult.setCode("1");
            jsonResult.setData(userOrders);
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }

    @PostMapping("/selectAllOrderCount")
    @ApiOperation("返回所有订单的数量")
    public JsonResult selectAllOrderCount(){
        JsonResult jsonResult = new JsonResult();
        Integer integer = userOrderService.selectAllOrderCount();
        if (integer!=0){
            jsonResult.setCode("1");
            jsonResult.setData(integer);
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }

    @PostMapping("/getDetailByUserOrderId")
    @ApiOperation("通过订单id得到订单详情")
    @ApiImplicitParam(name = "id",example = "6")
    public JsonResult getDetailByUserOrderId(@RequestParam Integer id){
        JsonResult jsonResult = new JsonResult();
        UserOrder detailByUserOrderId = userOrderService.getDetailByUserOrderId(id);

        if (detailByUserOrderId!=null){
            jsonResult.setCode("1");
            jsonResult.setData(detailByUserOrderId);
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }

    @PostMapping("/reviewAndupdateUserOrder")
    @ApiOperation("审核和修改订单")
    @ApiImplicitParam(name = "userOrder")
    public JsonResult reviewAndupdateUserOrder(@RequestBody UserOrder userOrder){
        JsonResult jsonResult = new JsonResult();
        Integer integer = userOrderService.reviewAndupdateUserOrder(userOrder);
        if (integer==1){
            jsonResult.setCode("1");
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }
}
